package com.learn.demo.dto;

import java.util.ArrayList;
import com.learn.demo.entity.User;
import com.learn.demo.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * 用户查询
 */
@Repository
public class UserDto {

	@Autowired
	private final JdbcTemplate jdbcTemplate;

	public UserDto(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


	public void addUser(User user) {
		String sql = "insert into t_user(username, password) values(?, ?)";
		jdbcTemplate.update(sql, user.getUsername(), user.getPassword());
	}

	public ArrayList<User> getUsers() {
		String sql = "select id,username,password from t_user";
		ArrayList<User> users = (ArrayList<User>) jdbcTemplate.query(sql, new UserMapper());

		return users;
	}
}
